{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d8c709f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "import requests\n",
    "import datetime\n",
    "import hashlib\n",
    "import base64\n",
    "import hmac\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "30dff35f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 讯飞机器翻译平台 API\n",
    "class get_result(object):\n",
    "    def __init__(self,host):\n",
    "        # 应用ID（到控制台获取）\n",
    "        self.APPID = \"5a1e4350\"\n",
    "        # 接口APISercet（到控制台机器翻译服务页面获取）\n",
    "        self.Secret = \"fd253abab96c2c42457451688437d06d\"\n",
    "        # 接口APIKey（到控制台机器翻译服务页面获取）\n",
    "        self.APIKey= \"9d418b0693142dd3c3d96cdf51edbbfc\"\n",
    "        \n",
    "        \n",
    "        # 以下为POST请求\n",
    "        self.Host = host\n",
    "        self.RequestUri = \"/v2/ots\"\n",
    "        # 设置url\n",
    "        # print(host)\n",
    "        self.url=\"https://\"+host+self.RequestUri\n",
    "        self.HttpMethod = \"POST\"\n",
    "        self.Algorithm = \"hmac-sha256\"\n",
    "        self.HttpProto = \"HTTP/1.1\"\n",
    "\n",
    "        # 设置当前时间\n",
    "        curTime_utc = datetime.datetime.utcnow()\n",
    "        self.Date = self.httpdate(curTime_utc)\n",
    "        # 设置业务参数\n",
    "        # 语种列表参数值请参照接口文档：https://www.xfyun.cn/doc/nlp/niutrans/API.html\n",
    "        self.Text=\"How old are you\"\n",
    "        self.BusinessArgs={\n",
    "                \"from\": \"en\",\n",
    "                \"to\": \"ja\",\n",
    "            }\n",
    "\n",
    "    def hashlib_256(self, res):\n",
    "        m = hashlib.sha256(bytes(res.encode(encoding='utf-8'))).digest()\n",
    "        result = \"SHA-256=\" + base64.b64encode(m).decode(encoding='utf-8')\n",
    "        return result\n",
    "\n",
    "    def httpdate(self, dt):\n",
    "        \"\"\"\n",
    "        Return a string representation of a date according to RFC 1123\n",
    "        (HTTP/1.1).\n",
    "\n",
    "        The supplied date must be in UTC.\n",
    "\n",
    "        \"\"\"\n",
    "        weekday = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"][dt.weekday()]\n",
    "        month = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\",\n",
    "                 \"Oct\", \"Nov\", \"Dec\"][dt.month - 1]\n",
    "        return \"%s, %02d %s %04d %02d:%02d:%02d GMT\" % (weekday, dt.day, month,\n",
    "                                                        dt.year, dt.hour, dt.minute, dt.second)\n",
    "\n",
    "    def generateSignature(self, digest):\n",
    "        signatureStr = \"host: \" + self.Host + \"\\n\"\n",
    "        signatureStr += \"date: \" + self.Date + \"\\n\"\n",
    "        signatureStr += self.HttpMethod + \" \" + self.RequestUri \\\n",
    "                        + \" \" + self.HttpProto + \"\\n\"\n",
    "        signatureStr += \"digest: \" + digest\n",
    "        signature = hmac.new(bytes(self.Secret.encode(encoding='utf-8')),\n",
    "                             bytes(signatureStr.encode(encoding='utf-8')),\n",
    "                             digestmod=hashlib.sha256).digest()\n",
    "        result = base64.b64encode(signature)\n",
    "        return result.decode(encoding='utf-8')\n",
    "\n",
    "    def init_header(self, data):\n",
    "        digest = self.hashlib_256(data)\n",
    "        #print(digest)\n",
    "        sign = self.generateSignature(digest)\n",
    "        authHeader = 'api_key=\"%s\", algorithm=\"%s\", ' \\\n",
    "                     'headers=\"host date request-line digest\", ' \\\n",
    "                     'signature=\"%s\"' \\\n",
    "                     % (self.APIKey, self.Algorithm, sign)\n",
    "        #print(authHeader)\n",
    "        headers = {\n",
    "            \"Content-Type\": \"application/json\",\n",
    "            \"Accept\": \"application/json\",\n",
    "            \"Method\": \"POST\",\n",
    "            \"Host\": self.Host,\n",
    "            \"Date\": self.Date,\n",
    "            \"Digest\": digest,\n",
    "            \"Authorization\": authHeader\n",
    "        }\n",
    "        return headers\n",
    "\n",
    "    def get_body(self):\n",
    "        content = str(base64.b64encode(self.Text.encode('utf-8')), 'utf-8')\n",
    "        postdata = {\n",
    "            \"common\": {\"app_id\": self.APPID},\n",
    "            \"business\": self.BusinessArgs,\n",
    "            \"data\": {\n",
    "                \"text\": content,\n",
    "            }\n",
    "        }\n",
    "        body = json.dumps(postdata)\n",
    "        #print(body)\n",
    "        return body\n",
    "\n",
    "    def call_url(self):\n",
    "        if self.APPID == '' or self.APIKey == '' or self.Secret == '':\n",
    "            print('Appid 或APIKey 或APISecret 为空！请打开demo代码，填写相关信息。')\n",
    "        else:\n",
    "            code = 0\n",
    "            body=self.get_body()\n",
    "            headers=self.init_header(body)\n",
    "            #print(self.url)\n",
    "            response = requests.post(self.url, data=body, headers=headers,timeout=8)\n",
    "            status_code = response.status_code\n",
    "            #print(response.content)\n",
    "            if status_code!=200:\n",
    "                # 鉴权失败\n",
    "                print(\"Http请求失败，状态码：\" + str(status_code) + \"，错误信息：\" + response.text)\n",
    "                print(\"请根据错误信息检查代码，接口文档：https://www.xfyun.cn/doc/nlp/niutrans/API.html\")\n",
    "            else:\n",
    "                # 鉴权成功\n",
    "                respData = json.loads(response.text)\n",
    "                # print(respData)\n",
    "                # 以下仅用于调试\n",
    "                code = str(respData[\"code\"])\n",
    "                if code!='0':\n",
    "                    print(\"请前往https://www.xfyun.cn/document/error-code?code=\" + code + \"查询解决办法\")\n",
    "                return respData"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "eeeacbeb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 机器翻译封装方法\n",
    "def translate(text='你好世界', source='cn', target='uy'):\n",
    "    ##示例:  host=\"ntrans.xfyun.cn\"域名形式\n",
    "    host = \"ntrans.xfyun.cn\"\n",
    "    #初始化类\n",
    "    gClass=get_result(host)\n",
    "    gClass.Text = text\n",
    "    gClass.BusinessArgs['from'] = source\n",
    "    gClass.BusinessArgs['to'] = target\n",
    "    result = gClass.call_url()\n",
    "    return result['data']['result']['trans_result']['dst']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aea147a9",
   "metadata": {},
   "outputs": [],
   "source": [
    "cat_cn_df = pd.read_csv('../datasets/商品分类关键词_汉语.csv', encoding='utf-8', header=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0f307e19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>keywords</th>\n",
       "      <th>Unnamed: 1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>游戏话费</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>服装鞋包</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>手机数码</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>家用电器</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>美妆饰品</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  keywords  Unnamed: 1\n",
       "0     游戏话费         NaN\n",
       "1     服装鞋包         NaN\n",
       "2     手机数码         NaN\n",
       "3     家用电器         NaN\n",
       "4     美妆饰品         NaN"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_cn_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3b9c4b79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 生成关键词列表\n",
    "keywords_cn = cat_cn_df['keywords'].tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bed9b895",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['游戏话费', '服装鞋包', '手机数码', '家用电器', '美妆饰品']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "keywords_cn[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1bceabaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "keywords_cn2uy = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "6d45e711",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "169669"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(keywords_cn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d34846c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 游戏话费 ئويۇن تېلېفون ھەققى\n",
      "1 服装鞋包 كىيىم - كېچەك ئاياغ سومكا\n",
      "2 手机数码 يان تېلېفون رەقەملىك\n",
      "3 家用电器 ئائىلە ئېلېكتر سايمانلىرى\n",
      "4 美妆饰品 چىرايلىق پەرداز بۇيۇملىرى\n",
      "5 母婴用品 ئانا - بالىلار بۇيۇملىرى\n",
      "6 家居建材 ئۆي جاھازلىرى، قۇرۇلۇش ماتېرىياللىرى\n",
      "7 百货食品 تۈرلۈك ماللار يېمەكلىك\n",
      "8 运动户外 تەنھەرىكەت دالا\n",
      "9 文化玩乐 مەدەنىي كۆڭۈل ئېچىش\n",
      "保存临时文件...\n",
      "10 生活服务 تۇرمۇش مۇلازىمىتى\n",
      "11 其他商品 باشقا تاۋار\n",
      "12 汽配摩托 ئاپتوموبىل زاپچاسلىرى موتور\n",
      "13 腾讯QQ专区 تېڭشۈن QQ ۋىلايەتلىك\n",
      "14 网络游戏点卡 تور ئويۇنى نۇقتىسى كارتىسى\n",
      "15 网游装备/游戏币/帐号/代练 تور ئويۇنى جابدۇق / ئويۇن پۇل / ھېسابات نومۇرى / ۋاكالىتەن مەشىق\n",
      "16 服饰配件/皮带/帽子/围巾 كىيىم - كېچەك زاپچاسلىرى / تاسما / قالپاق / شارپا\n",
      "17 流行男鞋 مودا ئەرەنچە ئاياغ\n",
      "18 男装 ئەرەنچە كىيىم\n",
      "19 女士内衣/男士内衣/家居服 ئايالچە ئىچ كىيىم / ئەرلەر ئىچ كىيىمى / ئۆي جاھازلىرى كىيىمى\n",
      "保存临时文件...\n",
      "20 女鞋 ئايالچە ئاياقلار\n",
      "21 女装/女士精品 ئايالچە كىيىم - كېچەك / خانىم نادىر\n",
      "22 箱包皮具/热销女包/男包 چامادان - خۇرۇم بۇيۇملار / بازىرى ئىتتىك ئاياللار سومكىسى / ئەرلەر سومكىسى\n",
      "23 3C数码配件 3 C رەقەملىك زاپچاسلىرى\n",
      "24 DIY电脑 كومپيۇتېرDIY\n",
      "25 MP3/MP4/iPod/录音笔 MP3/MP4/iPod/ئۈن ئېلىش قەلىمى\n",
      "26 办公设备/耗材/相关服务 ئىشخانا ئۈسكۈنىلىرى / ماتېرىيال / ئالاقىدار مۇلازىمەت\n",
      "27 笔记本电脑 خاتىرە كومپيۇتېر\n",
      "28 电脑硬件/显示器/电脑周边 كومپيۇتېر قاتتىق دېتال / كۆرسەتكۈچ / كومپيۇتېر ئەتراپى\n",
      "29 电玩/配件/游戏/攻略 ئېلېكتىرونلۇق ئويۇنچۇق / زاپچاسلىرى / ئويۇن / ھۇجۇم تاكتىكىسى { ئېلېكترون }\n",
      "保存临时文件...\n",
      "30 电子元器件市场 ئېلېكترونلۇق دېتال بازىرى\n",
      "31 二手数码 نىمكەش رەقەملىك\n",
      "32 品牌台机/品牌一体机/服务器 ماركا سۇپىسى ماشىنىسى / ماركا بىر گەۋدىلەشكەن ماشىنىسى / مۇلازىمەتچى\n",
      "33 平板电脑/MID تەكشى ئېكرانلىق كومپيۇتېر /MID\n",
      "34 闪存卡/U盘/存储/移动硬盘 چاقنىما ساقلىغۇچ كارتىسى / بارماق دېسكا / ساقلاش / كۆچمە قاتتىق دىسكا\n",
      "35 手机 يان تېلېفون\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-42fa15823733>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mkeywords_cn2uy_temp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mkeywords_cn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0mtx_cn2uy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtranslate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msource\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'cn'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtarget\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'uy'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mkeywords_cn2uy_temp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtx_cn2uy\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtx_cn2uy\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-3-0411df8cd4c9>\u001b[0m in \u001b[0;36mtranslate\u001b[1;34m(text, source, target)\u001b[0m\n\u001b[0;32m      8\u001b[0m     \u001b[0mgClass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mBusinessArgs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'from'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msource\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m     \u001b[0mgClass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mBusinessArgs\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'to'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtarget\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m     \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mgClass\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcall_url\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     11\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'data'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'trans_result'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'dst'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-2-e5cb5243ea58>\u001b[0m in \u001b[0;36mcall_url\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    103\u001b[0m             \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minit_header\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    104\u001b[0m             \u001b[1;31m#print(self.url)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 105\u001b[1;33m             \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrequests\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpost\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    106\u001b[0m             \u001b[0mstatus_code\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstatus_code\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    107\u001b[0m             \u001b[1;31m#print(response.content)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mpost\u001b[1;34m(url, data, json, **kwargs)\u001b[0m\n\u001b[0;32m    114\u001b[0m     \"\"\"\n\u001b[0;32m    115\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 116\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'post'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    117\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    118\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\requests\\api.py\u001b[0m in \u001b[0;36mrequest\u001b[1;34m(method, url, **kwargs)\u001b[0m\n\u001b[0;32m     58\u001b[0m     \u001b[1;31m# cases, and look like a memory leak in others.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     59\u001b[0m     \u001b[1;32mwith\u001b[0m \u001b[0msessions\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSession\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 60\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     61\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     62\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36mrequest\u001b[1;34m(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)\u001b[0m\n\u001b[0;32m    531\u001b[0m         }\n\u001b[0;32m    532\u001b[0m         \u001b[0msend_kwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mupdate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msettings\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 533\u001b[1;33m         \u001b[0mresp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprep\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0msend_kwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    534\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    535\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mresp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\requests\\sessions.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, request, **kwargs)\u001b[0m\n\u001b[0;32m    644\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    645\u001b[0m         \u001b[1;31m# Send the request\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 646\u001b[1;33m         \u001b[0mr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0madapter\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    647\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    648\u001b[0m         \u001b[1;31m# Total elapsed time of the request (approximately)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\requests\\adapters.py\u001b[0m in \u001b[0;36msend\u001b[1;34m(self, request, stream, timeout, verify, cert, proxies)\u001b[0m\n\u001b[0;32m    447\u001b[0m                     \u001b[0mdecode_content\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    448\u001b[0m                     \u001b[0mretries\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmax_retries\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 449\u001b[1;33m                     \u001b[0mtimeout\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    450\u001b[0m                 )\n\u001b[0;32m    451\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36murlopen\u001b[1;34m(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)\u001b[0m\n\u001b[0;32m    670\u001b[0m                 \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    671\u001b[0m                 \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 672\u001b[1;33m                 \u001b[0mchunked\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mchunked\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    673\u001b[0m             )\n\u001b[0;32m    674\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_make_request\u001b[1;34m(self, conn, method, url, timeout, chunked, **httplib_request_kw)\u001b[0m\n\u001b[0;32m    374\u001b[0m         \u001b[1;31m# Trigger any extra validation we need to do.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    375\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 376\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_validate_conn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    377\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mSocketTimeout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mBaseSSLError\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    378\u001b[0m             \u001b[1;31m# Py2 raises this as a BaseSSLError, Py3 raises it as socket timeout.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\urllib3\\connectionpool.py\u001b[0m in \u001b[0;36m_validate_conn\u001b[1;34m(self, conn)\u001b[0m\n\u001b[0;32m    992\u001b[0m         \u001b[1;31m# Force connect early to allow us to validate the connection.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    993\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mconn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"sock\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# AppEngine might not have  `.sock`\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 994\u001b[1;33m             \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    995\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    996\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mis_verified\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\urllib3\\connection.py\u001b[0m in \u001b[0;36mconnect\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    332\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    333\u001b[0m         \u001b[1;31m# Add certificate verification\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 334\u001b[1;33m         \u001b[0mconn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_new_conn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    335\u001b[0m         \u001b[0mhostname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhost\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    336\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\urllib3\\connection.py\u001b[0m in \u001b[0;36m_new_conn\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    155\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    156\u001b[0m             conn = connection.create_connection(\n\u001b[1;32m--> 157\u001b[1;33m                 \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_dns_host\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mport\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mextra_kw\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    158\u001b[0m             )\n\u001b[0;32m    159\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\envs\\Python\\Python37\\lib\\site-packages\\urllib3\\util\\connection.py\u001b[0m in \u001b[0;36mcreate_connection\u001b[1;34m(address, timeout, source_address, socket_options)\u001b[0m\n\u001b[0;32m     72\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0msource_address\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     73\u001b[0m                 \u001b[0msock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbind\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msource_address\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m             \u001b[0msock\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msa\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     75\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0msock\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     76\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "keywords_cn2uy_temp = []\n",
    "for i, tx in enumerate(keywords_cn):\n",
    "    tx_cn2uy = translate(text=tx, source='cn', target='uy')\n",
    "    keywords_cn2uy_temp.append(tx_cn2uy)\n",
    "    print(i+1, tx, tx_cn2uy)\n",
    "    if i > 0 and ((i+1) % 10 == 0 or (i+1) == len(keywords_cn)):\n",
    "        print('保存临时文件...')\n",
    "        keywords_cn2uy_temp_df = pd.DataFrame({'keywords_cn2':keywords_cn[i-9 : i+1], 'keywords_cn2uy':keywords_cn2uy_temp})\n",
    "        keywords_cn2uy_temp_df.to_csv('../datasets/商品分类关键词_汉语转维语_'+ str(i+1) +'.csv', encoding='utf-8-sig', header=0, index = False)\n",
    "        keywords_cn2uy += keywords_cn2uy_temp\n",
    "        keywords_cn2uy_temp = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "1cedad8c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'keywords_cn2uy' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-94a4e5944faf>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mkeywords_cn2uy\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name 'keywords_cn2uy' is not defined"
     ]
    }
   ],
   "source": [
    "keywords_cn2uy[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e659504c",
   "metadata": {},
   "outputs": [],
   "source": [
    "keywords_cn2uy_df = pd.DataFrame({'keywords_cn2':keywords_cn, 'keywords_cn2uy':keywords_cn2uy})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c5c55a3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "keywords_cn2uy_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80d0e36b",
   "metadata": {},
   "outputs": [],
   "source": [
    "keywords_cn2uy_df.to_csv('../datasets/商品分类关键词_汉语转维语.csv', encoding='utf-8-sig', header=0, index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "384ee9cf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
